<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/31
 * Time: 10:58
 */

namespace app\api\home;


/** 收藏夹
 * Class Collect
 * @package app\api\home
 */
class Collect extends Common
{
    // 收藏夹列表
    public function lists()
    {
        $user_id = input('get.user_id',0);
        if($user_id == 0){
            return _json(401, '请先登录',[]);
        }
        $coll_list = db('collect')->alias('c')->join('goods g','g.id=c.value_id')
            ->field('c.*,g.goods_name,g.shop_price,g.goods_brief,g.thumb_url')->where('c.user_id',$user_id)->order('c.add_time desc')->select();
        foreach ($coll_list as $k=>$v){
            $coll_list[$k]['thumb_url'] = 'http://'. $host = $_SERVER['HTTP_HOST'].get_file_path($v['thumb_url']);
        }
        $data = [
          'collectList' => $coll_list
        ];

        return _json(0, '执行成功',$data);
    }

    // 添加或删除
    public function addordelete()
    {
        $user_id = input('post.user_id',0);
        $type_id = input('post.typeId',0);
        $value_id = input('post.valueId',0);
        if(!$user_id){
            return _json(401, '请先登录',[]);
        }
        $has = db('collect')->where([
            'user_id'=>$user_id,
            'type_id'=>$type_id,
            'value_id'=>$value_id
        ])->find();
        if($has){
            $res = db('collect')->where([
                'user_id'=>$user_id,
                'type_id'=>$type_id,
                'value_id'=>$value_id
            ])->delete();
            $type = 'del';
        }else{
            $do = [
                'user_id' => $user_id,
                'type_id' => $type_id,
                'value_id' => $value_id,
                'add_time' => time()
            ];
            $type = 'add';
            $res = db('collect')->data($do)->insert();
        }
        if($res){
            return _json(0, '执行成功',['type'=>$type]);
        }else{
            return _json(-1, '操作失败');
        }
    }
}